package com.sen.controller;


import com.sen.dto.TypeDTO;
import com.sen.entity.Type;
import com.sen.result.Result;
import com.sen.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sen
 * @since 2021-03-13
 */
@Api(tags = "类别控制器")
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService  typeService;

    @ApiOperation(value = "查询所有类别列表")
    @GetMapping("/list/{cur}/{size}")
    public Result getTypeList(@PathVariable Integer cur, @PathVariable Integer size){
        List<Type> typeList = typeService.getTypeList(cur, size);
        return Result.ok().data("data", typeList);
    }

    @ApiOperation(value = "查询类别信息")
    @GetMapping("/{id}")
    public Result getTypeById(@PathVariable Integer id){
        Type type = typeService.getTypeById(id);
        return Result.ok().data("data", type);
    }

    @ApiOperation(value = "新增类别")
    @PostMapping
    public Result addType(@RequestBody TypeDTO typeDTO){
        boolean b = typeService.saveType(typeDTO);
        if (b){
            return Result.ok().msg("添加成功");
        }
        else{
            return Result.error().msg("添加失败");
        }
    }

    @ApiOperation(value = "删除类别")
    @DeleteMapping("/{id}")
    public Result deleteTypeById(@PathVariable Integer id){
        boolean b = typeService.deleteTypeById(id);
        if (b){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @ApiOperation(value = "更新类别")
    @PutMapping
    public Result updateType(@RequestBody TypeDTO typeDTO){
        boolean b = typeService.updateType(typeDTO);
        if (b){
            return Result.ok().msg("更新成功");
        }else{
            return Result.error().msg("更新失败");
        }
    }
}
